package com.qf.service.caiwu_service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.Account;
import com.qf.bean.AccountItem;
import com.qf.dao.caiwu_dao.AccountItemMapper;
import com.qf.dao.caiwu_dao.AccountMapper;
import com.qf.service.caiwu_service.AccountItemService;
import com.qf.utils.caiwu.vo.PrefundReportVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileFilter;
import java.util.List;
import java.util.Map;

/**
 * 2019/12/11 0011
 * Author->DT
 * sassspringboot
 * 行到水穷处 坐看云起时
 * 模块：
 */
@Service
public class AccountItemServiceImpl implements AccountItemService {
    @Resource
    private AccountItemMapper accountItemMapper;
    @Resource
    private AccountMapper accountMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        //遍历删除子事项
       this.deleteAll(id);
        //查询id对应的事项
        AccountItem accountItem = accountItemMapper.selectByPrimaryKey(id);
        if (accountItem==null){
            return 1;
        }else
            return 0;
    }


    /**
     * 定义一个方法，递归删除和某级菜单有关联的下级菜单
     */

    public  void deleteAll(Integer parentId){
        //查询以该id为上级id的子事项
        List<AccountItem> accountItemList=accountItemMapper.selectByParentId(parentId);
        if (accountItemList!=null){
            for (AccountItem accountItem : accountItemList) {
                deleteAll(accountItem.getId());
            }
        }
        //删除无子事项的对应事项
        accountItemMapper.deleteByPrimaryKey(parentId);
        //删除对应事项id的账目信息
        accountMapper.deleteByItemId(parentId);
    }


    @Override
    public int insert(AccountItem record) {
        return accountItemMapper.insert(record);
    }

    @Override
    public AccountItem selectByPrimaryKey(Integer id) {
        AccountItem accountItem = accountItemMapper.selectByPrimaryKey(id);
        return accountItem;
    }

    @Override
    public int updateByPrimaryKeySelective(AccountItem record) {
        return accountItemMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<AccountItem> selectAll() {
        return accountItemMapper.selectAll();
    }

    //分页查询事项
    @Override
    public PageInfo<AccountItem> findItemListByLimit(Map map) {
        //开启分页
        PageHelper.startPage((int)map.get("index"),(int)map.get("page"));
        //分页查询事项列表
        List<AccountItem> list=accountItemMapper.findItemList(map);
        //pageInfo
        PageInfo<AccountItem> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
